Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knowage 8043 #891

Merged
merged 22 commits into from
Sep 20, 2023
Merged

Knowage 8043 #891

merged 22 commits into from
Sep 20, 2023

Conversation

BojanSovticEngIT
Copy link
Contributor

Files changed:

  • knowage-core/src/main/java/it/eng/spagobi/analiticalmodel/document/service/ManagePreviewFileAction.java - Should we remove FileUtils.checkPathTraversalAttack?
  • knowage-core/src/main/java/it/eng/spagobi/api/DossierActivityResource.java
  • knowage-core/src/main/java/it/eng/spagobi/api/SelfServiceDataSetCRUD.java
  • knowage-core/src/main/java/it/eng/spagobi/sdk/utilities/SDKObjectsConverter.java
  • knowagebirtreportengine/src/main/java/it/eng/spagobi/engines/birt/BirtImageServlet.java - Shoud we remove custom Path traversal check inside this method?
  • knowagebirtreportengine/src/main/java/it/eng/spagobi/engines/birt/utilities/Utils.java
  • knowagecockpitengine/src/main/java/it/eng/knowage/engine/cockpit/api/export/excel/ExcelExporter.java
  • knowagecommonjengine/src/main/java/it/eng/spagobi/engines/commonj/runtime/WorksRepository.java
  • knowagejasperreportengine/src/main/java/it/eng/spagobi/engines/jasperreport/JasperReportRunner.java
  • knowagemeta/src/main/java/it/eng/knowage/meta/generator/jpamapping/JpaMappingClassesGenerator.java
  • knowagetalendengine/src/main/java/it/eng/spagobi/engines/talend/runtime/JobDeploymentDescriptor.java - Method is not being used anywhere, we can only check file name I think?
  • knowagetalendengine/src/main/java/it/eng/spagobi/engines/talend/runtime/RuntimeRepository.java
  • knowageutils/src/main/java/it/eng/spagobi/tools/dataset/utils/datamart/DefaultEngineDatamartRetriever.java
  • knowageutils/src/main/java/it/eng/spagobi/utilities/SpagoBIAccessUtils.java
  • knowageutils/src/test/java/it/eng/spagobi/security/utils/PasswordEncryptionToolController.java

@davide-zerbetto davide-zerbetto added the security Security related issues label Sep 13, 2023
Copy link

@davide-zerbetto davide-zerbetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes are required allover modified files

Comment on lines 112 to 116
try {
PathTraversalChecker.get(fileName, targetDirectory.getName());
} catch (Exception e) {
throw new PathTraversalAttackException("Error managing preview file for file: " + fileName);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PathTraversalChecker should throw only PathTraversalAttackException exception, that is runtime exception, so we can remove the try catch bloch

PathTraversalChecker.get(fileName, targetDirectory.getName());
} catch (Exception e) {
throw new PathTraversalAttackException("Error managing preview file for file: " + fileName);
}
FileUtils.checkPathTraversalAttack(fileName, targetDirectory);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this method and use PathTraversalChecker instead


java.nio.file.Path outputPath = Paths.get(SpagoBIUtilities.getResourcePath(), "dossier", String.valueOf(documentId), fileName);
File file = outputPath.toFile();
String outPath = SpagoBIUtilities.getResourcePath() + separator + "dossier" + separator + documentId + separator + fileName;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use PathTraversalChecker.get method:
PathTraversalChecker.get(SpagoBIUtilities.getResourcePath() + separator + "dossier" , documentId, fileName)

Comment on lines 1633 to 1635
throw new PathTraversalAttackException(
"Error getting removind/moving dataset file for orginal file " + originalFileName + " and new file " + newFileName);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this part

@@ -37,6 +37,25 @@ private PathTraversalChecker() {
throw new IllegalStateException("This class provides utility methods. It cannot be instantiated");
}

public static void get(String firstDirectory, String... otherFolders) throws PathTraversalAttackException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename firstDirectory as "trustedDirectory" or "safeDirectory"

Comment on lines 52 to 55
} catch (Exception e) {
logger.error(e);
throw new PathTraversalAttackException(e.getMessage());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code is not throwing any exception (apart from runtime PathTraversalAttackException), we can remove the try catch block

Comment on lines 1758 to 1760
} catch (Exception e) {
throw new PathTraversalAttackException("Error deleting dataset file " + fileName);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this catch block

Requested pull changes done. (Removed try/catch for PathTraversalChecker methods, renamed PathTraversalChecker.get property name.
@@ -107,7 +108,7 @@ public void doService() {
// checks for path traversal attacks
private void checkRequiredFile(String fileName) {
File targetDirectory = GeneralUtilities.getPreviewFilesStorageDirectoryPath();
FileUtils.checkPathTraversalAttack(fileName, targetDirectory);
PathTraversalChecker.get(fileName, targetDirectory.getName());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't make sense: inputs are reversed, since targetDirectory is the safe dir and filename is what has to be checked

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

JSONObject response = new JSONObject();
File dossierDir = new File(SpagoBIUtilities.getResourcePath() + separator + "dossier" + separator + documentId + separator);
try {
PathTraversalChecker.isValidFileName(fileName);
PathTraversalChecker.preventPathTraversalAttack(file, dossierDir);
PathTraversalChecker.get(fileName, dossierDir.getName());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputs reversed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -171,8 +169,7 @@ public Response checkPathFile(@QueryParam("templateName") String fileName, @Quer
File dossierDir = new File(SpagoBIUtilities.getResourcePath() + separator + "dossier" + separator + documentId + separator);
JSONObject response = new JSONObject();
try {
PathTraversalChecker.isValidFileName(fileName);
PathTraversalChecker.preventPathTraversalAttack(file, dossierDir);
PathTraversalChecker.get(fileName, dossierDir.getName());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputs reversed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

File newDatasetFile = new File(fileNewPath + newFileName + "." + fileType.toLowerCase());
if (originalDatasetFile.exists()) {

if (newDatasetFile != null && originalDatasetFile != null && originalDatasetFile.exists()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how files can be null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

File datasetFile = new File(filePath + fileName);
if (datasetFile.exists()) {

if (datasetFile != null && datasetFile.exists()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how files can be null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

PathTraversalChecker.isValidFileName(fileName);
File attachment = new File(fileName);

if (attachment != null && attachment.exists() && attachment.isFile()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how files can be null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

File attachment = new File(dh.getName());
if (attachment.exists() && attachment.isFile()) {
String fileName = dh.getName();
PathTraversalChecker.isValidFileName(fileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileName seems to be a complete path instead of a single file name. Please check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to talk about this in today's call.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert the changes, most likely this is a false positive

logger.error("File " + imageFile.getPath() + " not found");
return;
}
PathTraversalChecker.get(parent.toString(), fileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parent is NOT a safe dir, imageDirectory or imageTmpDir are safe.
Filename is a complete path, don't see how PathTraversalChecker.get will work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to talk about this in today's call.

Comment on lines 134 to 135
PathTraversalChecker.get(safeDirectory, fileName);
String outPath = safeDirectory + separator + fileName;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge them in 1 line: PathTraversalChecker.get has to return a File or something similar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

ResponseBuilder responseBuilder = null;

File file = new File(outPath);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge this with the above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 1624 to 1625
PathTraversalChecker.get(filePath, originalFileName);
File originalDatasetFile = new File(filePath + originalFileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge those lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 1626 to 1627
PathTraversalChecker.get(fileNewPath, newFileName + "." + fileType.toLowerCase());
File newDatasetFile = new File(fileNewPath + newFileName + "." + fileType.toLowerCase());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge those lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

File attachment = new File(dh.getName());
if (attachment.exists() && attachment.isFile()) {
String fileName = dh.getName();
PathTraversalChecker.isValidFileName(fileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert the changes, most likely this is a false positive

Comment on lines 92 to 93
PathTraversalChecker.get(fileName, directory);
htmlFile = new File(directory, fileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge those 2 lines

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use
File htmlFile = PathTraversalChecker.get(BirtReportServlet.OUTPUT_FOLDER, reportExecutionId, fileName);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 84 to 85
PathTraversalChecker.get(rootDir.getName(), fileName);
File workDir = new File(rootDir, fileName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge those 2 lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Changed preventPathTraversalAttack method to private, fixed in other files except DossierActivityResource.
Updated PathTraversalChecker.get
Updated PathTraversalChecker.get inside importTemplateFile method
Fixed htmlFile = PathTraversalChecker.get
Chages reverted, false positive
Moved PathTraversal check to the calling function in excel exporter
PathTraversal fixes
Changes with Davide Z. (Meet)
@davide-zerbetto davide-zerbetto merged commit dc6c9b1 into KnowageLabs:master Sep 20, 2023
1 check passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
security Security related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants